package com.doeis.manage.service;

import java.util.ArrayList;
import java.util.List;

import com.doeis.core.db.DBException;
import com.doeis.core.db.DBManager;
import com.doeis.core.db.DaoException;
import com.doeis.manage.bean.Catalog;
import com.doeis.manage.bean.Page;
import com.doeis.manage.dao.PageDao;

public class PageService {
	private static final PageDao dao = new PageDao();

	public List<Page> list(int cat_id) throws Exception {
		try {
			String sql = "select * from " + Page.tableName + " where cat_id=? ";
			List<Object> params = new ArrayList<Object>();
			params.add(cat_id);
			return dao.query(Page.class, sql, false, params.toArray());
		} catch (DBException e) {
			throw new Exception(e);
		}finally{
			DBManager.closeAllBindConnection();
		}
	}

	public Page findById(int id) throws Exception {
		try {
			return dao.readById(Page.class, id);
		} catch (DaoException e) {
			throw new Exception(e);
		}finally{
			DBManager.closeAllBindConnection();
		}
	}

	public void update(Page page) throws Exception {
		try {
			dao.update(page);
		} catch (DaoException e) {
			throw new Exception(e);
		}finally{
			DBManager.closeAllBindConnection();
		}
	}

	public void add(Page page) throws Exception {
		try {
			dao.insert(page);
		} catch (DaoException e) {
			throw new Exception(e);
		}finally{
			DBManager.closeAllBindConnection();
		}
	}

	public boolean delete(int id) throws Exception {
		try {
			return dao.delete(Catalog.class, id);
		} catch (DaoException e) {
			throw new Exception(e);
		}finally{
			DBManager.closeAllBindConnection();
		}
	}
}
